package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Role;
import com.ebring.shareEducation.bean.SysparamInfo;
import com.ebring.shareEducation.bean.Traderecords;
import com.ebring.shareEducation.bean.User;
import com.ebring.shareEducation.dao.SysparaMapper;
import com.ebring.shareEducation.dao.TraderecordsMapper;
import com.ebring.shareEducation.service.SystemParamService;
import com.ebring.shareEducation.service.TraderecordsService;
import com.ebring.shareEducation.utils.Md5Utils;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class SysparamInfoServiceImpl implements SystemParamService {


    @Autowired
    private SysparaMapper sysparaMapper;


    @Override
    public RspMsg<SysparamInfo> insertsyspara(SysparamInfo sysparamInfo) {
        RspMsg rspMsg = new RspMsg();

        int i = sysparaMapper.insertSelective(sysparamInfo);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("新增失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("新增成功");

        return rspMsg;
    }

    @Override
    public RspMsg<SysparamInfo> updatesyspara(SysparamInfo sysparamInfo) {
        RspMsg rspMsg = new RspMsg();
        SysparamInfo sysparamInfo1 =sysparaMapper.selectByPrimaryKey(sysparamInfo.getParaId());

        if(sysparamInfo.getParaType()!=null&&!"".equals(sysparamInfo.getParaType())){
            sysparamInfo1.setParaType(sysparamInfo.getParaType());
        }
        if(sysparamInfo.getParaName()!=null&&!"".equals(sysparamInfo.getParaName())){
            sysparamInfo1.setParaName(sysparamInfo.getParaName());
        }

        if(sysparamInfo.getSrcParaId()!=null&&!"".equals(sysparamInfo.getSrcParaId())){
            sysparamInfo1.setSrcParaId(sysparamInfo.getSrcParaId());
        }

        int i = sysparaMapper.updateByPrimaryKeySelective(sysparamInfo1);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("更新信息失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("更新信息成功");

        return rspMsg;
    }

    @Override
    public RspMsg<SysparamInfo> selectsysparaById(String id) {
        Example example = new Example(Role.class);

        if (id != null && !"".equals(id)){
            example.createCriteria().andEqualTo("paraId", "%"+id+"%");
        }

        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List sysList = sysparaMapper.selectByExample(example);
        map.put("sysList", sysList);

        if (sysList.isEmpty() || sysList == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到相关用户");
            return rspMsg;
        }else{
            rspMsg.setOK();
            rspMsg.setResult(map);
            return rspMsg;
        }
    }

    @Override
    public RspMsg<SysparamInfo> selectsysparafind() {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<SysparamInfo> sysList = sysparaMapper.selectAll();
        if (sysList.isEmpty() || sysList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }
        PageInfo<SysparamInfo> pageInfo = new PageInfo<SysparamInfo>(sysList);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("sysList", sysList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<List<SysparamInfo>> findsyspara(SysparamInfo sysparamInfo) {
        return null;
    }
}
